import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

public class _1两数之和 {

    public int[] twoSum(int[] nums, int target) {
        for (int i = 0; i < nums.length; i++) {
            int value = nums[i];
            int result = target - value;
            for (int j = i + 1; j < nums.length; j++) {
                if (result == nums[j]) {
                    return new int[]{i, j};
                }
            }
        }
        return null;
    }

    public int[] twoSumHash(int[] nums, int target) {
        Map<Integer, Integer> map = new HashMap<>();
        for (int i = 0; i < nums.length ; i++) {
            map.put(nums[i], i);
        }
        for (int i = 0; i < nums.length ; i++) {
            int ivalue = nums[i];
            int result = target - ivalue;
            Integer index = map.get(result);
            if (index != null && index != i) {
                return new int[]{i, index};
            }
        }
        return null;

    }


    public static void main(String[] args) {
        _1两数之和 man = new _1两数之和();
        int[] ints = new int[]{3, 2, 4};
        int[] ints1 = man.twoSumHash(ints, 6);
        for (int i : ints1) {
            System.out.println(i);
        }
    }


}
